package com.yidu.BusinessData.controller;




import com.yidu.BusinessData.entity.CashEntity;
import com.yidu.BusinessData.service.CashService;
import com.yidu.CashAdministration.entity.MoneyAllocateEntity;
import com.yidu.Util.util.Dbutil;
import com.yidu.Util.util.TableIdUtil;
import com.yidu.Util.util.TableNameUtil;
import com.yidu.Util.util.TableTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class CashController {

    @Resource
    private CashService cashService;
    @Autowired
    Dbutil dbutil;
    /**
     * 添加
     */
    @RequestMapping("/insertCash")
    public void insertCash(CashEntity cash, HttpServletRequest req){
        System.out.println("进入添加");
        HttpSession session = req.getSession();
        String fundId = (String) session.getAttribute("fundId");
        cash.setFundId(fundId);
        String maxId = dbutil.getMaxId3(TableIdUtil.CID, TableNameUtil.C, TableTimeUtil.CTime,cash.getEndDate());
        cash.setCashId(maxId);
        cashService.insertCash(cash);
    }
    /**
     * 进入首页
     */
    @RequestMapping("pageCash")
    public Map selectPage(int page,int limit,CashEntity cashEntity){
        Map maps = new HashMap();
        List<CashEntity> CashEntityList = cashService.selectCash(page,limit,cashEntity);
        int i = cashService.CashCount(cashEntity);
        maps.put("code",0);
        maps.put("msg","");
        maps.put("count",i);
        maps.put("data",CashEntityList);
        System.out.println("shouye"+cashEntity);
        return maps;
    }

    /**
     * 删除
     * @param ids
     */
    @RequestMapping("deleteCash")
    public Object  deleteCash(String ids){
        String id[] = ids.split(",");
        MoneyAllocateEntity a = new MoneyAllocateEntity();
        a.setAllocateFlow(1);
        Map map = new HashMap();
        try{
            for (int i = 0; i < id.length; i++) {
                String moneyAllocateId  = id[i];
                cashService.deleteCash(moneyAllocateId);
                map.put("count", 1);
            }
        }catch (Exception e) {
            e.printStackTrace();
            map.put("count", 2);
        }
        System.out.println(ids+"你好");

        return "";
    }

    /**
     * 修改
     * @param cashEntity
     */
    @RequestMapping("updateCash")
    public void updateCash(CashEntity cashEntity){
        cashService.updateCash(cashEntity);
    }

}
